import { Service } from 'egg';
import { Op } from 'sequelize';

interface UserRegisterParams {
  username: string;
  password: string;
}

interface UserGetherParams {
  userId: number;
  gender: '男' | '女',
  avatar: string;
  nickname: string;
  area: string;
  birthday: string;
  height: number;
  motto: string;
}

type UserLoginParams = UserRegisterParams;

export default class User extends Service {
  async register(registerInfo: UserRegisterParams) {
    const { ctx } = this;
    const { username, password } = registerInfo;
    const user = await ctx.model.User.create({ username, password });
    return await ctx.helper.success({
      userId: user.getDataValue('userId'),
    });
  }

  async gether(getherInfo: UserGetherParams) {
    const { ctx } = this;
    const user = await ctx.model.User.findByPk(getherInfo.userId);
    if (!user) {
      return await ctx.helper.fail('用户不存在');
    }
    await ctx.model.User.update({
      gender: getherInfo.gender,
      avatar: getherInfo.avatar,
      nickname: getherInfo.nickname,
      area: getherInfo.area,
      birthday: getherInfo.birthday,
      height: getherInfo.height,
      motto: getherInfo.motto,
    }, {
      where: { userId: getherInfo.userId },
    });
    const updatedUser = await ctx.model.User.findByPk(getherInfo.userId);
    const target: any = updatedUser!.toJSON();
    delete target.password;
    return await ctx.helper.success(target, '更新用户信息成功');
  }

  async login(loginInfo: UserLoginParams) {
    const { ctx } = this;
    const { username, password } = loginInfo;
    const user = await ctx.model.User.findOne({
      where: { username, password },
    });
    if (!user) {
      return ctx.helper.fail('用户名或密码错误');
    }
    const userId = user.getDataValue('userId');
    ctx.cookies.set('userId', String(userId), {
      maxAge: 1000 * 60 * 60 * 24, // 24 小时
    });
    const target: any = user.toJSON();
    delete target.password;
    return ctx.helper.success(target, '登录成功');
  }

  async logout() {
    const { ctx } = this;
    ctx.cookies.set('userId', null, {
      maxAge: 0,
    });
    return ctx.helper.success('退出成功');
  }

  async detail(userId: number) {
    const { ctx } = this;
    const user = await ctx.model.User.findOne({
      where: { userId },
    });
    if (!user) {
      return ctx.helper.fail('用户不存在');
    }
    const target: any = user.toJSON();
    delete target.password;
    return ctx.helper.success(target);
  }

  async near(userId: number) {
    const { ctx } = this;
    const user = await ctx.model.User.findByPk(userId);
    if (!user) {
      return ctx.helper.fail('用户不存在');
    }
    const area = user._attributes.area;
    const nears = await ctx.model.User.findAll({
      where: {
        area,
        userId: {
          [Op.not]: user._attributes.userId,
        },
      },
    });
    return ctx.helper.success(nears);
  }

  async online(userId: number) {
    // FIXME: 接入 websocket 后改为在线人数
    const { ctx } = this;
    return await ctx.model.User.findAll({
      where: {
        userId: {
          [Op.not]: userId,
        },
      },
    });
  }
}
